/*
 * Copyright 2010 Gal Dolber.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.unnison.framework.test.mvp;

import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.user.client.Command;

import com.unnison.framework.client.GuitPresenter;
import com.unnison.framework.client.async.AbstractAsyncCallback;
import com.unnison.framework.client.binder.EventBusHandler;
import com.unnison.framework.client.binder.GuitBinder;
import com.unnison.framework.client.binder.ViewField;
import com.unnison.framework.client.binder.ViewHandler;
import com.unnison.framework.client.binder.contributor.PreventDefault;
import com.unnison.framework.client.binder.contributor.RunAsync;
import com.unnison.framework.client.binder.contributor.StopPropagation;
import com.unnison.framework.client.binder.contributor.UserAction;
import com.unnison.framework.client.place.Place;
import com.unnison.framework.test.Asserts;
import com.unnison.framework.test.action.OneAction;
import com.unnison.framework.test.action.OneResponse;
import com.unnison.framework.test.mvp.OnePresenter.OneBinder;
import com.unnison.framework.test.mvp.event.AssertEvent;

public class OnePresenter extends GuitPresenter<OneBinder> implements Place<OneData> {

    public interface OneBinder extends GuitBinder<OnePresenter> {
    }

    @ViewField
    public ButtonI button1;

    @ViewField
    public ButtonI button2;

    @ViewField
    public ButtonI eventBubbleButton;

    @ViewField
    public ButtonI stopEventBubbleButton;

    @ViewField
    public ButtonI userActionButton;

    @ViewField
    public HasHandlers widgetWithEvents;

    @EventBusHandler
    public void $context() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_EVENTBUSCONTEXTEVENT));
    }

    @ViewHandler
    public void button1$button2$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_MULTIPLEFIELDS));
    }

    @ViewHandler
    @RunAsync
    protected void button1$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.valueOf(button1.getText())));
        eventBus.fireEvent(new AssertEvent(Asserts.valueOf(button2.getText())));
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_BUTTON1CLICK));
    }

    @ViewHandler
    @RunAsync
    protected void button2$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_BUTTON2CLICK));
    }

    @ViewHandler
    public void eventBubbleButton$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_BUBBLEBUTTON));
    }

    @ViewHandler
    public void eventBubbleWidget$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_BUBBLEPANEL));
    }

    @Override
    public void go(OneData data) {
        eventBus.fireEvent(new AssertEvent(Asserts.valueOf(data.getS())));
    }

    public void sendAction(final Command command) {
        commandService.execute(new OneAction(), new AbstractAsyncCallback<OneResponse>() {
            @Override
            public void success(OneResponse result) {
                if (result.getAssert().equals(Asserts.RPCOK)) {
                    command.execute();
                }
            }
        });
    }

    @ViewHandler
    @StopPropagation
    @PreventDefault
    public void stopEventBubbleButton$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_STOPBUBBLEBUTTON));
    }

    // Never fired (if everything its ok)
    @ViewHandler
    public void stopEventBubbleWidget$click() {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_STOPBUBBLEPANEL));
    }

    @ViewHandler
    @UserAction("USERACTION")
    public void userActionButton$click() {
    }

    @ViewHandler
    public void widgetWithEvents$one(String eventValue, String data$name, boolean eventPrimitiveBoolean, Boolean eventNonPrimitiveBoolean) {
        eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_DIRECTVIEW));
        eventBus.fireEvent(new AssertEvent(Asserts.valueOf(eventValue)));
        if (eventPrimitiveBoolean) {
            eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_EVENTPRIMITIVEBOOLEAN));
        }
        if (eventNonPrimitiveBoolean.equals(false)) {
            eventBus.fireEvent(new AssertEvent(Asserts.ONEPRESENTER_EVENTNONPRIMITIVEBOOLEAN));
        }
        eventBus.fireEvent(new AssertEvent(Asserts.valueOf(data$name)));
    }
}
